package com.xinou.daily_report.controller;


import com.alibaba.excel.EasyExcel;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xinou.common.base.config.ApiResponse;
import com.xinou.common.base.exception.BusException;
import com.xinou.common.enumerate.HttpStatusEnum;
import com.xinou.daily_report.PO.daily.*;
import com.xinou.daily_report.VO.*;
import com.xinou.daily_report.VO.daily.DailyInfoVO;
import com.xinou.daily_report.VO.daily.DailyListVO;
import com.xinou.daily_report.VO.daily.DeductDateVO;
import com.xinou.daily_report.entity.SysCalendar;
import com.xinou.daily_report.service.*;
import com.xinou.service.file.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-06-11
 */
@RestController
@RequestMapping("/admin/daily")
@Api(tags = {"后台 日报"})
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysDailyController {

    final SysDailyService sysDailyService;
    final SysDailyCommentService sysDailyCommentService;
    final SysFlowerService sysFlowerService;
    private final FileUploadService fileUploadService;
    final SysIntegralService sysIntegralService;
    public static final String UploadFileUrl="https://image.xiaokesi.com/";
    final SysCalendarService sysCalendarService;

    @PostMapping("save")
    @ApiOperation("添加")
    @RequiresPermissions({"admin:daily:save"})
    public ApiResponse<Boolean> dailyAdd(@RequestBody DailyAddPO po){
        return sysDailyService.dailyAdd(po);
    }

    @PostMapping("list")
    @ApiOperation("获取列表")
    @RequiresPermissions({"admin:daily:list"})
    public ApiResponse<DailyListVO> dailyList(@RequestBody DailyListPO po){
        return sysDailyService.dailyList(po);
    }

    @PostMapping("singleList")
    @ApiOperation("获取任务条款列表")
    @RequiresPermissions({"admin:daily:singleList"})
    public ApiResponse<DailySingleListVO> dailySingleList(@RequestBody DailySingleListPO po){
        return sysDailyService.dailySingleList(po);
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    @RequiresPermissions({"admin:daily:delete"})
    public ApiResponse<Boolean> dailyDelete(@RequestBody DailyDeletePO po){
        return sysDailyService.dailyDelete(po);
    }

    @PostMapping("update")
    @ApiOperation("更新")
    @RequiresPermissions({"admin:daily:update"})
    public ApiResponse<String> dailyUpdate(@RequestBody DailyUpdatePO po){
        return sysDailyService.dailyUpdate(po);
    }

    @PostMapping("commentList")
    @ApiOperation("获取点评列表")
    @RequiresPermissions({"admin:daily:commentList"})
    public ApiResponse<DailyCommentInfoVO> dailyCommentList(@RequestBody DailyCommentListPO po){
        return sysDailyCommentService.dailyCommentList(po);
    }

    @PostMapping("myCommentList")
    @ApiOperation("获取我的点评列表")
    @RequiresPermissions({"admin:daily:myCommentList"})
    public ApiResponse<MyCommentListVO> myCommentList(@RequestBody MyCommentListPO po){
        return sysDailyCommentService.myCommentList(po);
    }

    @PostMapping("info")
    @ApiOperation("日报详情")
    @RequiresPermissions({"admin:daily:info"})
    public ApiResponse<DailyInfoVO> dailyInfo(@RequestBody DailyInfoPO po){
        return sysDailyService.dailyInfo(po);
    }

    @PostMapping("comment")
    @ApiOperation("添加评论")
    @RequiresPermissions({"admin:daily:comment"})
    public ApiResponse<String> commentSave(@RequestBody DailyCommentAddPO po){
        return sysDailyCommentService.dailyComment(po);
    }

    @PostMapping("reply")
    @ApiOperation("添加回复")
    @RequiresPermissions({"admin:daily:reply"})
    public ApiResponse<String> replySave(@RequestBody DailyReplyPO po){
        return sysDailyCommentService.dailyReply(po);
    }

    @PostMapping("scoreAdd")
    @ApiOperation("给日报加分")
    @RequiresPermissions({"admin:daily:scoreAdd"})
    public ApiResponse<Void> scoreAdd(@RequestBody DailyScoreAddPO po){
        return sysIntegralService.integralDailyScoreAdd(po);
    }

    @PostMapping("giveFlower")
    @ApiOperation("给日报送花")
    @RequiresPermissions({"admin:daily:giveFlower"})
    public ApiResponse<String> giveFlower(@RequestBody DailyFlowerAddPO po){
        return sysFlowerService.giveFlower(po);
    }

    @PostMapping("flowerList")
    @ApiOperation("送花列表")
    @RequiresPermissions({"admin:daily:flowerList"})
    public ApiResponse<DailyFlowerInfoVO> flowerList(@RequestBody DailyFlowerListPO po){
        return sysFlowerService.flowerList(po);
    }

    @ApiOperation(value = "日报下载")
    @PostMapping("down")
    @ApiOperationSupport(ignoreParameters = {"po.id"})
    public ApiResponse<String> userSignListDown(@RequestBody DailyListPO po) throws IOException {
        List<DailyListExcelVO> excelUserVOS = sysDailyService.dailyDownList(po);
        System.out.println(excelUserVOS);
        File tempFile = File.createTempFile(String.valueOf(System.nanoTime()),".xlsx");
        EasyExcel.write(tempFile, DailyListExcelVO.class).sheet("日报").doWrite(excelUserVOS);
        String s = fileUploadService.fileUpload(tempFile).orElseThrow(() -> new BusException(HttpStatusEnum.RE_CODE_PARAM_ERROR, "生成日报失败"));
        System.out.println(UploadFileUrl+s);
        return new ApiResponse<>(UploadFileUrl+s);
    }

    @ApiOperation(value = "日历列表")
    @PostMapping("calendar")
    @RequiresPermissions({"admin:daily:calendar"})
    public ApiResponse<DeductDateVO> dailyCalendar(@RequestBody DeductDateListPO po) {
        return sysCalendarService.deductDateList(po);
    }

    @ApiOperation(value = "日历添加或者更新")
    @PostMapping("calendarChange")
    @RequiresPermissions({"admin:daily:calendarChange"})
    public ApiResponse<Void> dailyCalendarChange(@RequestBody DeductDatePO po) {
        return sysCalendarService.addOrUpdateDeductDate(po);
    }

}

